package com.ticket.sass.admin.dto.output.order;

import com.ticket.sass.admin.model.KeyInfoModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author ywb
 * @date 2024-03-25 16:06
 */
@Data
@ApiModel("卖品信息")
public class CommodityOutput {
    @ApiModelProperty(value = "名称")
    private String name;
    
    @ApiModelProperty(value = "封面图", required = true)
    private List<KeyInfoModel> cover;
    
    @ApiModelProperty(value = "价格")
    private BigDecimal price;
    
    @ApiModelProperty(value = "数量")
    private Integer num;
    
    @ApiModelProperty(value = "卖品规格信息")
    private String skuInfo;
    
    @ApiModelProperty(value = "单价")
    private BigDecimal unitPrice;
    
    
    public BigDecimal getUnitPrice() {
        if (num == null || num == 0) {
            return new BigDecimal("0.0");
        }
        return price.divide(new BigDecimal(num), 2, RoundingMode.HALF_UP);
    }
    
    /**
     * 获取卖品信息的描述，格式为 "名称 单价 x数量 = 总价元"。
     * @return 格式化后的卖品信息字符串。
     */
    public String getCommodityInfo() {
        // 检查数量是否为1，如果是，则不显示数量，只显示单价
        String quantityPart = (num != null && num == 1) ? "" : " x" + num;
        
        // 构建卖品信息字符串
        return name + " " + price.setScale(2, RoundingMode.HALF_UP) + "元" + quantityPart + " = " + price.setScale(2, RoundingMode.HALF_UP) + "元";
    }
}
